package com.biubiu.biubiuexam.services;

import cn.dev33.satoken.stp.StpUtil;
import com.biubiu.biubiuexam.common.dto.LoginDto;
import com.biubiu.biubiuexam.common.dto.RegisterDto;
import com.biubiu.biubiuexam.common.dto.UserInfoDto;
import com.biubiu.biubiuexam.common.lang.Result;
import com.biubiu.biubiuexam.sql.entity.User;
import com.biubiu.biubiuexam.sql.entity.UserGroup;
import com.biubiu.biubiuexam.sql.repository.UserGroupRepository;
import com.biubiu.biubiuexam.sql.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.util.Date;

/**
 * 账户相关 Service.
 */
@Service
public class AccountService {

    private final UserRepository userRepository;

    private final UserGroupRepository userGroupRepository;

    /**
     * 自动装配构造函数
     *
     * @param userRepository      UserRepository 的实例
     * @param userGroupRepository UserGroupRepository 的实例
     */
    @Autowired
    public AccountService(UserRepository userRepository, UserGroupRepository userGroupRepository) {
        this.userRepository = userRepository;
        this.userGroupRepository = userGroupRepository;
    }

    /**
     * 注册接口
     *
     * @param registerDto 注册所需信息
     * @return 注册结果 result
     */
    public Result register(RegisterDto registerDto) {
        User temp = userRepository.findByEmail(registerDto.getEmail());
        if (temp == null) {
            Date date = new Date();
            DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);

            String formattedDate = dateFormat.format(date);

            UserGroup userGroup = userGroupRepository.findUserGroupByName("student");
            if (userGroup == null) {
                userGroup = new UserGroup("student", "normal", "student");
                userGroupRepository.save(userGroup);
            }

            User user = new User(
                    registerDto.getPassword(),
                    registerDto.getEmail(),
                    registerDto.getNickName(),
                    formattedDate,
                    registerDto.getDescription(), userGroup);

            userRepository.save(user);

            StpUtil.login(user.getId());

            return Result.success(StpUtil.getTokenInfo());
        } else {
            return Result.fail(403, "邮箱已被注册", null);
        }
    }

    /**
     * 登录接口
     *
     * @param loginDto 登录所需信息
     * @return 登录结果 result
     */
    public Result login(LoginDto loginDto) {

        User user = userRepository.findByEmail(loginDto.getEmail());
        if (user == null) {
            return Result.fail(403, "用户不存在！", null);
        }

        if (!user.getPassword().equals(loginDto.getPassword())) {
            return Result.fail(403, "密码错误！", null);
        }

        StpUtil.login(user.getId());

        return Result.success(StpUtil.getTokenInfo());
    }

    /**
     * 获取某用户的信息
     *
     * @param email 用户邮箱
     * @return 查询结果 user info
     */
    public Result getUserInfo(String email) {
        User user = userRepository.findByEmail(email);
        if (user == null) {
            return Result.fail(403, "用户不存在！", null);
        }
        UserInfoDto userInfo = new UserInfoDto();
        userInfo.setNickName(user.getNickName());
        userInfo.setDescription(user.getDescription());
        userInfo.setEmail(user.getEmail());
        return Result.success(userInfo);
    }

    /**
     * 获取自己的信息
     *
     * @param userId 用户 ID
     * @return 查询结果 my user info
     */
    public Result getMyUserInfo(Long userId) {
        User user = userRepository.getById(userId);
        UserInfoDto userInfo = new UserInfoDto();
        userInfo.setNickName(user.getNickName());
        userInfo.setDescription(user.getDescription());
        userInfo.setEmail(user.getEmail());
        return Result.success(userInfo);
    }

    /**
     * 检查自己是否拥有某身份
     *
     * @param role 身份
     * @return 结果
     */
    public Result isRole(String role) {
        if (!StpUtil.hasRole(role)) {
            return Result.fail(200, "您没有这个身份！", null);
        } else {
            return Result.success(200, "您拥有这个身份！", null);
        }
    }
}
